//
//  GQModifyNameController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/6.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQModifyNameController.h"
#import "GQRegisterDetailController.h"
#import "GQRegisterInfo.h"
#import "TipView.h"
#import "GQProfileTool.h"
#import "GQLoginResult.h"
#import "GQLoginUserResult.h"

#define kModeifyMargin 20
#define kModeifyHeight 44

@interface GQModifyNameController ()

@property (weak, nonatomic) UITextField *textField;

@end

@implementation GQModifyNameController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, kNavigatationBarHeight, kScreenWidth, kScreenHeight - kNavigatationBarHeight);
    self.navigationItem.title = @"姓名";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"distribute_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToPre)];
    
    UIButton *rightItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 20)];
    [rightItem setTitle:@"保存" forState:UIControlStateNormal];
    [rightItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightItem.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightItem addTarget:self action:@selector(saveItemDidClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
    [self setUpModifyTextField];
}

- (void)saveItemDidClick
{
    
    NSString *text = [self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSRange range = [text rangeOfString:@" "];
    if (range.length) {
        [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"有非法字符"];
    }else{
        [GQRegisterInfo sharedGQRegisterInfo].name = text;
        if ([self.type isEqualToString:@"edit"]){
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            [param setObject:[GQLoginResult sharedGQLoginResult].user.userId forKey:@"userId"];
            [param setObject:text forKey:@"nick"];
            [GQProfileTool profileModifyUserInfo:param modifyType:@"2" success:^(id result) {
                NSInteger errorCode = [[result objectForKey:@"errorCode"] integerValue];
                if (errorCode == 0) {
                    [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_smile"] tipText:@"修改成功"];
                }else{
                    [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"失败成功"];
                }
                
            } failure:^(NSError *error) {
                
            }];
            
        }else{
            [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_smile"] tipText:@"保存成功"];
        }
    }
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpModifyTextField
{

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, kModeifyHeight)];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 74, self.view.width, kModeifyHeight)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.leftView = leftView;
    textField.text = self.name;
    textField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:textField];
    self.textField = textField;
}

@end
